import scrapy
import xlwt,xlrd
import time
import pandas as pd
from datetime import datetime
# scrapy runspider myspider.py  运行指令



class WriteExcel():
    def __init__(self,sheet_name,excel_name):
        # 创建一个workbook 设置编码  和worksheet
        self.wb = xlwt.Workbook(encoding='utf-8')
        self.ws = self.wb.add_sheet(sheet_name)
        self.excel_name = excel_name

    def write_excel_body(self, count, cmd, asrTextInputTime, wsRevAsrTime, asrIntentTime, asrStartReqTime, startRes, tatalCost, midWareReturn):
        # 数据写入excel,参数对应 行, 列, 值
        self.ws.write(count, 0, cmd)
        self.ws.write(count, 1, asrTextInputTime)
        self.ws.write(count, 2, wsRevAsrTime)
        self.ws.write(count, 3, asrIntentTime)
        self.ws.write(count, 4, asrStartReqTime)
        self.ws.write(count, 5, startRes)
        self.ws.write(count, 6, tatalCost)
        self.ws.write(count, 7, midWareReturn)

    def write_excel_head(self):

        self.ws.write(0, 0, '指令')
        self.ws.write(0, 1, 'asr文案输入')
        self.ws.write(0, 2, 'ws接收asr时刻')
        self.ws.write(0, 3, 'asr识别完时刻')
        self.ws.write(0, 4, '发起asr接口时刻')
        self.ws.write(0, 5, '开始响应时刻')
        self.ws.write(0, 6, '总耗时')
        self.ws.write(0, 7, '中间件返回内容')

    def save_excel(self):
        current_time = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime(time.time()))
        self.wb.save('/Users/v_baiguanghui/Downloads/云控相关项目/101.测试脚本/bigScreenDigitalPeople/result/data_{}_{}.xls'.format(self.excel_name,current_time))


class MySpider(scrapy.Spider):
    name = 'myspider'
    start_urls = ['file:///Users/v_baiguanghui/Downloads/%E6%8E%A7%E5%B1%8F-%E5%A4%8D%E6%B5%8B.html']
    def parse(self, response):

        writeObj = WriteExcel('Sheet', '控屏')
        writeObj.write_excel_head()

        links = response.css('body > div > details').getall()
        for count in range(1,len(links)+1):
                cmd = response.css('body > div > details:nth-child({}) > summary::text'.format(count)).get().split('：')[1]
                fullCmd = response.css('body > div > details:nth-child({}) > summary::text'.format(count)).get()
                if fullCmd == "指令：" or fullCmd.startswith("指令：{"):    # 语音输入为空
                    continue
                else:
                    midWareReturn = response.css('body > div > details:nth-child({}) > dl > div:nth-child(4) > dd > pre::text'.format(count)).get()
                    index_type = midWareReturn.find('type')
                    index_typeEnd = midWareReturn.find('"',index_type+8)
                    if midWareReturn[index_type+8:index_typeEnd] == '控屏':

                        asrTextInputTime = response.css('body > div > details:nth-child({}) > dl > div:nth-child(1) > dt::text'.format(count)).get().split('- ')[1]
                        wsRevAsrTime = response.css('body > div > details:nth-child({}) > dl > div:nth-child(2) > dt::text'.format(count)).get().split('- ')[1]
                        asrIntentTime = response.css('body > div > details:nth-child({}) > dl > div:nth-child(2) > dd > pre::text'.format(count)).get().split('"time": ')[1][0:13]
                        asrStartReqTime = response.css('body > div > details:nth-child({}) > dl > div:nth-child(3) > dt::text'.format(count)).get().split('- ')[1]
                        midWareReturn = response.css('body > div > details:nth-child({}) > dl > div:nth-child(4) > dd > pre::text'.format(count)).get()

                        index_res = midWareReturn.find('开始响应')
                        index_resTime = midWareReturn.find('timestamp',index_res)
                        startRes = midWareReturn[index_resTime+13:index_resTime+26]
                        endTime = response.css('body > div > details:nth-child({}) > dl > div:nth-child(5) > dt::text'.format(count)).get().split('- ')[1]


                        dt = datetime.strptime(asrTextInputTime, "%Y/%m/%d %H:%M:%S")
                        startTime = int(dt.timestamp()) * 1000

                        tatalCost = int(startRes) - int(startTime)
                        # print(timestamp - asrIntentTime)

                        writeObj.write_excel_body(count, cmd, asrTextInputTime, wsRevAsrTime, asrIntentTime, asrStartReqTime, startRes,
                             tatalCost, midWareReturn)

        writeObj.save_excel()

if __name__ == '__main__':
    import sys

    if len(sys.argv) > 1:
        start_url = sys.argv[1]
    else:
        start_url = 'file:///Users/v_baiguanghui/Downloads/aaa.html'

    spider = MySpider()
    spider.start_urls = [start_url]
    spider.run()

